package sk.vasicek.drinkmanager.activity;

import java.util.Locale;

import sk.vasicek.drinkmanager.R;
import sk.vasicek.drinkmanager.domain.Language;
import android.app.ActionBar;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {

	public static boolean localeChanged = false;

	@Override
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
		if (!localeChanged)
			localeChanged = false;
		if (key.equals("LangPref")) {
			Configuration config = getBaseContext().getResources().getConfiguration();
			Locale locale = Language.getApplicationLanguage(this);
			Locale.setDefault(locale);
			config.locale = locale;
			getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
			localeChanged = true;
			finish();
			startActivity(getIntent());
		}
	}

	private static final String TAG = "DrinkManager_SettingsActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Configuration config = getBaseContext().getResources().getConfiguration();
		Locale locale = Language.getApplicationLanguage(this);
		Locale.setDefault(locale);
		config.locale = locale;
		getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics());
		this.addPreferencesFromResource(R.xml.drink_manager_settings);
		getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			ActionBar actionBar = getActionBar();
			actionBar.setDisplayHomeAsUpEnabled(true);
		}
		Log.d(TAG, "Opened settings screen");
	}

	@Override
	public void onBackPressed() {
		if (localeChanged) {
			AlarmManager alm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
			alm.set(AlarmManager.RTC, System.currentTimeMillis() + 500, PendingIntent.getActivity(this, 0, new Intent(this, DrinkManagerListActivity.class), 0));
			System.exit(2);
		}
		super.onBackPressed();
	}

	@Override
	protected void onStop() {
		super.onStop();
		getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		Log.d(TAG, "Closed settings screen");
	}

	@Override
	protected void onResume() {
		super.onResume();
		Log.d(TAG, "Returned to settings screen");
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = this.getMenuInflater();
		inflater.inflate(R.menu.optionmenu, menu);
		MenuItem settings = menu.findItem(R.id.settings);
		settings.setIntent(new Intent(this, SettingsActivity.class));
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle item selection
		switch (item.getItemId()) {
		case R.id.exit:
			SettingsActivity.this.finish();
			if (localeChanged) {
				AlarmManager alm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
				alm.set(AlarmManager.RTC, System.currentTimeMillis() + 500, PendingIntent.getActivity(this, 0, new Intent(this, DrinkManagerListActivity.class), 0));
				System.exit(2);
			}
			return true;
		case R.id.settings:
			Toast.makeText(SettingsActivity.this.getApplicationContext(), getResources().getText(R.string.alreadyToast), Toast.LENGTH_SHORT).show();
			return true;
		case android.R.id.home:
			// Toast.makeText(DrinkManagerListActivity.this.getApplicationContext(),
			// getResources().getText(R.string.alreadyToast),
			// Toast.LENGTH_SHORT).show();

			if (localeChanged) {
				AlarmManager alm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
				alm.set(AlarmManager.RTC, System.currentTimeMillis() + 500, PendingIntent.getActivity(this, 0, new Intent(this, DrinkManagerListActivity.class), 0));
				System.exit(2);
			} else {
				Intent intent = new Intent(this, DrinkManagerListActivity.class);
				intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				startActivity(intent);
			}
			return true;
		case R.id.drinks:
			if (localeChanged) {
				AlarmManager alm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
				alm.set(AlarmManager.RTC, System.currentTimeMillis() + 500, PendingIntent.getActivity(this, 0, new Intent(this, DrinkManagerListActivity.class), 0));
				System.exit(2);
			} else {
				Intent drinksIntent = new Intent(getApplicationContext(), DrinkManagerListActivity.class);
				this.startActivityForResult(drinksIntent, 0);
			}
			return true;
		case R.id.places:
			if (localeChanged) {
				AlarmManager alm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
				alm.set(AlarmManager.RTC, System.currentTimeMillis() + 500, PendingIntent.getActivity(this, 0, new Intent(this, DrinkPlacesActivity.class), 0));
				System.exit(2);
			} else {
				Intent placesIntent = new Intent(getApplicationContext(), DrinkPlacesActivity.class);
				this.startActivityForResult(placesIntent, 0);
			}

			return true;
		case R.id.custom:
			if (localeChanged) {
				AlarmManager alm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
				alm.set(AlarmManager.RTC, System.currentTimeMillis() + 500, PendingIntent.getActivity(this, 0, new Intent(this, CustomDrinksActivity.class), 0));
				System.exit(2);
			} else {
				Intent customIntent = new Intent(this, CustomDrinksActivity.class);
				this.startActivityForResult(customIntent, 0);
				// Toast.makeText(getApplicationContext(),
				// getResources().getText(R.string.alreadyToast),
				// Toast.LENGTH_SHORT).show();
			}
			return true;
		case R.id.history:
			if (localeChanged) {
				AlarmManager alm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
				alm.set(AlarmManager.RTC, System.currentTimeMillis() + 500, PendingIntent.getActivity(this, 0, new Intent(this, DrinkHistoryActivity.class), 0));
				System.exit(2);
			} else {
				Intent historyIntent = new Intent(this, DrinkHistoryActivity.class);
				this.startActivityForResult(historyIntent, 0);
				// Toast.makeText(getApplicationContext(),
				// getResources().getText(R.string.alreadyToast),
				// Toast.LENGTH_SHORT).show();
			}
			return true;
		case R.id.graphs:
			if (localeChanged) {
				AlarmManager alm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
				alm.set(AlarmManager.RTC, System.currentTimeMillis() + 500, PendingIntent.getActivity(this, 0, new Intent(this, DrinkGraphActivity.class), 0));
				System.exit(2);
			} else {
				Intent graphsIntent = new Intent(this, DrinkGraphActivity.class);
				this.startActivityForResult(graphsIntent, 0);
				// Toast.makeText(getApplicationContext(),
				// getResources().getText(R.string.alreadyToast),
				// Toast.LENGTH_SHORT).show();
			}
			return true;
		case R.id.back:
			if (localeChanged) {
				AlarmManager alm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
				alm.set(AlarmManager.RTC, System.currentTimeMillis() + 500, PendingIntent.getActivity(this, 0, new Intent(this, BackgroundChooserActivity.class), 0));
				System.exit(2);
			} else {
				Intent backIntent = new Intent(getApplicationContext(), BackgroundChooserActivity.class);
				this.startActivityForResult(backIntent, 0);
			}
			return true;
		case R.id.map:
			if (localeChanged) {
				AlarmManager alm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
				alm.set(AlarmManager.RTC, System.currentTimeMillis() + 500, PendingIntent.getActivity(this, 0, new Intent(this, MapsOverlayActivity.class), 0));
				System.exit(2);
			} else {
				Intent mapsIntent = new Intent(getApplicationContext(), MapsOverlayActivity.class);
				this.startActivityForResult(mapsIntent, 0);
			}
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}
